<?php declare(strict_types=1);

namespace App\Api\Middleware;

use App\Api\Constants\ErrorCode;
use App\Exception\BusinessException;
use Hyperf\Context\Context;
use App\Api\Constants\Business;
use Hyperf\HttpServer\Request;
use Hyperf\HttpServer\Response;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;

class  BasicMiddleware implements MiddlewareInterface
{

    #[Inject]
    protected Request $request;
    #[Inject]
    protected Response $response;

    protected LoggerInterface $logger;

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $accessChannel = $this->request->getHeader('Access-Channel')[0] ?? null;
        if (empty($accessChannel)) {
//            throw new BusinessException(ErrorCode::ERR_PARAMETER, 'Channel is missing');
        }
        Context::set(Business::CONTEXT_KEY_ACCEPT_CHANNEL, (int)$accessChannel);
        return $handler->handle($request);
    }

}